#include <stdio.h>
#include <stdlib.h>
#include "HEAD/intArray.h"
#define MAXSIZE 1024
typedef int elementype;

typedef struct SequenStack
{
    elementype data[MAXSIZE];
    int top;
} SequenStack;

// 初始化一个栈结构
SequenStack *Init_SequenStack()
{
    SequenStack *S = (SequenStack *)malloc(sizeof(SequenStack));
    if (S == NULL)
    {
        return 0;
    }
    S->top = -1;
    return S;
}

// 判断一个栈是否为空
int SequenStack_Empty(SequenStack *s)
{
    if (s->top == -1)
    {
        return 1;
    }

    return 0;
}

// 判断一个栈是否已满
int SequenStack_Full(SequenStack *s)
{
    if (s->top + 1 >= MAXSIZE)
    {
        return 1;
    }

    return 0;
}

// 求顺序栈的长度
int SequenStack_Length(SequenStack *s)
{
    if (s == NULL)
    {
        return 0;
    }

    return s->top + 1;
}

// 检查栈是否为空
int IsEmpty(SequenStack *s)
{
    if (s->top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// 检查栈是否已满
int IsFull(SequenStack *s)
{
    if (s->top + 1 >= MAXSIZE)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// 顺序栈的出栈
int Pop_SequenStack(SequenStack *s, elementype *x)
{

    if (!IsEmpty(s))
    {
        s->top--;
        *x = s->data[s->top + 1];
        return 1;
    }

    return 0;
}

// 顺序栈的入栈
int Push_SequenStack(SequenStack *s, elementype *x)
{

    if (!IsFull(s))
    {
        s->top++;
        s->data[s->top] = *x;
        return 1;
    }

    return 0;
}

int GetTop_SequenStack(SequenStack *s, elementype *x)
{
    if (!IsEmpty)
    {
        *x = s->data[s->top];
        return 1;
    }
    else
    {
        return 0;
    }
}

int Print_SequenStack(SequenStack *s)
{

    for (size_t i = 0; i < s->top + 1; i++)
    {
        printf("%3d ", s->data[i]);
    }
    putchar('\n');

    return 1;
}

int main()
{

    // 测试入栈
    int num = 10;
    int num1 = 11;
    int num2 = 12;
    int num3 = 13;
    SequenStack *s = Init_SequenStack();
    Push_SequenStack(s, &num);
    Push_SequenStack(s, &num1);
    Push_SequenStack(s, &num2);
    Push_SequenStack(s, &num3);
    printf("顺序栈入栈后:");
    Print_SequenStack(s);
    printf("此时栈顶我的位置是:%d\n\n", s->top);

    // 测试出栈
    int result;
    Pop_SequenStack(s, &result);
    printf("顺序栈出栈后:");
    Print_SequenStack(s);
    printf("此时栈顶我的位置是:%d\n\n", s->top);

    // 测试取栈顶
    GetTop_SequenStack(s, &result);
    printf("取栈顶的值为:%d\n",result);
    printf("此时栈顶我的位置是:%d\n\n", s->top);
    return 0;
}